(define tolerance 0.00001)

(define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))

(define (fixed-point f first-guess)
    (define (try guess cnt)
        (display cnt)
        (display ": ")
        (display guess)
        (newline)
        (let ((next (f guess)))
            (if (close-enough? guess next)
                next
                (try next (+ cnt 1)))))
    (try first-guess 1))

(define (find-x first-guess)
    (fixed-point (lambda (x) (/ (log 1000) (log x)))
                first-guess))

; x = log(1000)/log(x)
; 2x = x + log(1000)/log(x)
; x = (x + log(1000)/log(x))/2
(define (find-x-v2 first-guess)
    (fixed-point (lambda (x) (/ (+ x (/ (log 1000) (log x))) 2))
                first-guess))

(display "\n========================================\n")
; (display (fixed-point cos 1.0))
; (newline)
(display (find-x 10.0))
;不用平均阻尼，34 步
(display "\n========================================\n")
(display (find-x-v2 10.0))
;使用平均阻尼，11 步，使用平均阻尼有助于函数收敛
(display "\n========================================\n")
